package com.woniuxy.consumer.listener;

import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;

import com.woniuxy.consumer.entity.StoreInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RabbitListener(queues = "queue-store")
@Slf4j
public class AddStoreLinstener {
    @Autowired
    private RedisTemplate redisTemplate;


    //添加缓存
    @RabbitHandler
    public void handler(
            String str, Channel channel, Message message,
            @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag){

        StoreInfo  storeInfo= JSONUtil.toBean(str, StoreInfo.class);
        log.info("添加仓库监听器，JSON->StoreInfo,{}", storeInfo);
            // 清除缓存
//            redisTemplate.delete("storeList");
//            List<StoreInfo> storeDtos = storeInfoService.list();
        redisTemplate.opsForList().rightPush("storeList", storeInfo);

    }
}